#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/4/13 10:44
# @Author : 闫峰
# @File    : discount_listing_check.py
from utils.mysqlUtils.db_pool import Database

java_database = Database("jdwart_database")

sql1 = """
SELECT * FROM (	
SELECT 
		a.id listingId,
		a.category_id,
		a.category_name,
		a.item_id,
		a.item_sku,
		a.source_id,
		a.source_name,
		a.shop_id,
		a.currency,
		a.first_publish_time publish_time,
		DATEDIFF(a.first_publish_time, now()) 天数, 

	IF
		( b.sale IS NULL, 0, b.sale ) sale,
	IF
		( b.views IS NULL, 0, b.views ) views,
	IF
		( b.likes IS NULL, 0, b.likes ) likes,
	IF
		( b.comment_count IS NULL, 0, b.comment_count ) comment_count,
	IF
		( b.rating_star IS NULL, 0, b.rating_star ) rating_star,
		a.STATUS listingStatus 
	FROM
		test_zhcxkj_center_product.t_shopee_listing a
		LEFT JOIN test_zhcxkj_center_product.t_shopee_listing_extra b ON a.id = b.listing_id
-- 		LEFT JOIN t_shopee_listing_promotion slp ON slp.listing_id = sl.id 
	WHERE a.source_id=13996
	AND a.`status`=1
	AND a.is_deleted=0
	AND NOT EXISTS (SELECT 1 FROM test_zhcxkj_center_product.t_shopee_listing_promotion c WHERE a.id=c.listing_id AND c.`status`=0 AND c.is_deleted=0)
	GROUP BY a.item_id
	) t
WHERE 	
	1=1
-- 	AND t.category_id IN  (101709,101708,101707,101706)
-- 	 AND t.publish_time >= '2023-03-01 09:55:19' AND t.publish_time <= '2023-03-01 09:55:19' 
-- 	AND t.sale >= 0 
	AND t.sale <= 100
-- 	AND t.item_id='15897080041'
-- 	 AND t.views >= 225 AND t.views <= 225 
-- 	AND t.likes >= 974 AND t.likes <= 974
-- 	AND t.comment_count >= 68 AND t.comment_count <= 68 
-- 	AND t.rating_star >= 4.00 AND t.rating_star <= 4.00
ORDER BY
	t.listingId 
-- 	LIMIT 1000
"""

def diff_list(list1, list2):
    set1 = set(list1)
    set2 = set(list2)
    diff = list(set1 - set2) + list(set2 - set1)
    return diff

def func1():
    """验证批量创建活动功能"""
    except_list = []
    res1 = java_database.fetch_all(sql1, ())
    for i in res1:
        except_list.append(i['item_id'])

    promotion_id = ['74', '77', '73', '78', '76', '71', '75', '72', '70']
    sql2 = """SELECT item_id FROM test_zhcxkj_center_promotion.t_shopee_promotion_queue WHERE promotion_id=%s AND operation_type=2 GROUP BY item_id;"""
    actual_list = []
    for j in promotion_id:
        res2 = java_database.fetch_all(sql2, (int(j),))
        for itemid in res2:
            actual_list.append(itemid['item_id'])
    print("except_list的数量为{}".format(len(except_list)))
    print("actual_list的数量为{}".format(len(actual_list)))
    count = 0
    for i in except_list:
        if i in actual_list:
            count +=1
            pass
        else:
            print(f"{i}不在actual_list中")
    print(f"对比成功数据{count}")

    def find_duplicates(lst):
        duplicates = set(x for x in lst if lst.count(x) > 1)
        return list(duplicates)

    print(find_duplicates(actual_list))
    # if actual_list == except_list:
    #     pass
    # else:
    #     print("失败")


if __name__ == '__main__':
    func1()
